home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / xml / XmlDocument.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  4.9 KB  |  189 lines

  1. /****i* SOURCE_FILE/INFO
  2.     *
  3.     * NAME
  4.     *  XmlDocument.js
  5.     *
  6.     * USAGE
  7.     *  Part of Netobjects JavaScript Library.
  8.     *
  9.     * COPYRIGHT
  10.     *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.     *  All Rights Reserved.
  12.     *
  13.     *  This is an unpublished work protected by Website Pros, Inc.
  14.     *  as a trade secret, and is not to be used or disclosed except as
  15.     *  expressly provided in a written license agreement executed by
  16.     *  you and Website Pros, Inc.
  17.     *
  18.     *      <copyright@websitepros.com>
  19.     *
  20.     * NOTES
  21.     *  JavaScript code.
  22.     *
  23.     *****/
  24. if (!IS.isModuleInitialized("IS.NOF.XML.XmlDocument"))
  25. {
  26.     
  27.     /****h* NOF_JavaScript_Library/NOF.XML.XmlDocument
  28.     *
  29.     * NAME
  30.     *  NOF.XML.XmlDocument
  31.     *
  32.     * DESCRIPTION
  33.     *  
  34.     *
  35.     ****/
  36.     
  37.     /**
  38.     * Constructor    
  39.     */  
  40.     function XML_XmlDocument( ) {
  41.         this.__proto__ = XML_XmlDocument.prototype;                    
  42.     }
  43.     {
  44.         var member = XML_XmlDocument.prototype;    
  45.         
  46.         member.prefix        = null;
  47.         member.prefixes        = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
  48.         
  49.         var method = XML_XmlDocument.prototype;        
  50.  
  51.         /**
  52.         * Private method. Used to find the Automation server name
  53.         * Only for Microsoft's ActiveX.
  54.         * 
  55.         * @return 
  56.         **/                
  57.         method.getDomDocumentPrefix = function () {
  58.             if (this.prefix) {
  59.                 return ("" + this.prefix);
  60.             }
  61.             
  62.             var o;
  63.             for (var i = 0; i < this.prefixes.length; i++) {
  64.                 try {
  65.                     // try to create the objects
  66.                     o = new ActiveXObject(this.prefixes[i] + ".DomDocument");
  67.                     this.prefix = this.prefixes[i];
  68.                     return ("" + this.prefix);
  69.                 } catch (ex) {
  70.                 }
  71.             }
  72.             //alert("Could not find an installed XML parser");
  73.             throw new Error("Could not find an installed XML parser");
  74.             //throw new NOF.UTIL.Exception();
  75.         }
  76.         
  77.         /**
  78.         * Creates an instance of the DomDocument
  79.         * 
  80.         * @return 
  81.         **/
  82.         method.create = function (/*? document ?*/) {
  83.             try {
  84.                 if (window.ActiveXObject) {
  85.                     return new ActiveXObject(this.getDomDocumentPrefix() + ".DomDocument");
  86.                 }
  87.                 
  88.                 // DOM2
  89.                 if (document.implementation && document.implementation.createDocument) {
  90.                     var doc = document.implementation.createDocument("", "", null);
  91.                     
  92.                     // some versions of Moz do not support the readyState property
  93.                     // and the onreadystate event so we patch it!
  94.                     if (doc.readyState == null) {
  95.                         doc.readyState = 1;
  96.                         doc.addEventListener("load", function () {
  97.                             doc.readyState = 4;
  98.                             if (typeof(doc.onreadystatechange) == "function") {
  99.                                 doc.onreadystatechange();
  100.                             }
  101.                         }, false);
  102.                     }
  103.                     
  104.                     return doc;
  105.                 }
  106.             }
  107.             catch (ex) {}
  108.             //alert("Your browser does not support XmlDocument objects");
  109.             throw new Error("Your browser does not support XmlDocument objects");
  110.             //throw new NOF.UTIL.Exception();
  111.         }
  112.         
  113.         /**
  114.         * Create the loadXML method and xml getter for Mozilla
  115.         **/
  116.         if (window.DOMParser &&    window.XMLSerializer &&    window.Node && Node.prototype && Node.prototype.__defineGetter__) {
  117.                     
  118.             // XMLDocument did not extend the Document interface in some versions
  119.             // of Mozilla. Extend both!
  120.             //XMLDocument.prototype.loadXML = 
  121.             Document.prototype.loadXML = function (s) {
  122.                 
  123.                 // parse the string to a new doc    
  124.                 var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
  125.                 
  126.                 // remove all initial children
  127.                 while (this.hasChildNodes())
  128.                     this.removeChild(this.lastChild);
  129.                 
  130.                 // insert and import nodes
  131.                 for (var i = 0; i < doc2.childNodes.length; i++) {
  132.                     this.appendChild(this.importNode(doc2.childNodes[i], true));
  133.                 }
  134.             };
  135.             
  136.             
  137.             /*
  138.             * xml getter
  139.             *
  140.             * This serializes the DOM tree to an XML String
  141.             *
  142.             * Usage: var sXml = oNode.xml
  143.             *
  144.             */
  145.             // XMLDocument did not extend the Document interface in some versions
  146.             // of Mozilla. Extend both!
  147.             /*
  148.             XMLDocument.prototype.__defineGetter__("xml", function () {
  149.                 return (new XMLSerializer()).serializeToString(this);
  150.             });
  151.             */
  152.             Document.prototype.__defineGetter__("xml", function () {
  153.                 return (new XMLSerializer()).serializeToString(this);
  154.             });
  155.         }    
  156.         
  157.         /**
  158.         * Determine the prefix of the given namespace in a XML Document
  159.         * @param xmldoc - instance of the DOM document to search in.
  160.         * @param nsURL - namespace as URL
  161.         * @param nsPrefix - default prefix (returned if no other NS is found)
  162.         * @return prefix
  163.         **/
  164.         method.getNSPrefix = function (/*Document*/ xmldoc,/*String*/ nsURL,/*String*/ nsPrefix) {
  165.             var prefix = nsPrefix;
  166.             var node = xmldoc.documentElement;
  167.             //xmlns:lpb="http://www.netobjects.com/fusion/etc"
  168.             for (var i = 0; i < node.attributes.length; i++) {
  169.                 if (node.attributes[i].value == nsURL) {
  170.                     prefix = node.attributes[i].name;
  171.                     var j = prefix.indexOf("xmlns");
  172.                     if (j > -1) {
  173.                         j = prefix.indexOf(":", j);
  174.                         prefix = prefix.substring(j+1, prefix.length);
  175.                     }
  176.                     break;
  177.                 }
  178.             }
  179.             return prefix;
  180.         }
  181.         
  182.     }    
  183.     
  184.     NOF.XML.__proto__.XmlDocument = new XML_XmlDocument();    
  185. }
  186.     
  187.     
  188.     
  189.